/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Network;

import Burst.AGCH;
import Burst.RACH;
import Mobile.Mobile;
import java.util.ArrayList;

/**
 *
 * @author Javinel
 */
public class ThreadAGCHGenerator implements Runnable {
    
    private Network network;

    public Network getNetwork() {
        return network;
    }

    public void setNetwork(Network network) {
        this.network = network;
    }
    
    public ThreadAGCHGenerator(Network n){
        this.network=n;
    }

    @Override
    public void run() {
        while(true){
            while(network.getStateThreadAGCHgenerator()==1){
                while(!network.getRachReceived().isEmpty()){
                    for(RACH r:network.getRachReceived()){
                        for(int i=0;i<network.getUnusedSlots().length;i++){
                            if(network.getUnusedSlots()[i]==0){
                                AGCH ag=new AGCH(r.getIdMobile(),(i),(Network.getCounterFrame()+1));
                                Network.setCounterFrame(Network.getCounterFrame()+1);// maj counter de trame
                                network.setNextAGCH(ag); 
                                network.getUnusedSlots()[i]=1;
                                
                                network.setStateThreadAGCHgenerator(0); //désactivation du thread en attendant que le mobile récupère son agch
                                
                                for(Mobile m:network.getMobiles()){
                                    if(r.getIdMobile()==m.getId() && m.getSlotNumber()==-1){
                                        m.getAGCH(network); //envoi de l'agch au mobile
                                        break;
                                    }
                                }
                                break;
                            }
                        }
                        
                    }
                    //System.out.println("vide");
                    network.setRachReceived(new ArrayList<RACH>());//vide la liste des rach reçus et déjà traités
                }
            }
        }
    }
    
}
